import math

import gmpy2
import sympy


def len_consecutive_ab(a, b):
    for n in range(abs(b) + 1):
        t = n ** 2 + a * n + b
        if not gmpy2.is_prime(t):
            return n
    return -1


def search_a(b):
    max_n = 2
    res = 1
    for a in range(-999, 1000, 2):
        t = len_consecutive_ab(a, b)
        if t > max_n:
            max_n = t
            res = a * b
    return max_n, res


if __name__ == '__main__':
    b = 3
    max_consec = 1
    final_res = 1
    while b < 1000:
        t, product = search_a(b)
        if t > max_consec:
            max_consec = t
            final_res = product
        t, product = search_a(-b)
        if t > max_consec:
            max_consec = t
            final_res = product
        b = sympy.nextprime(b)
    print(final_res)
